<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="序号" prop="sortNum">
        <el-input-number  v-model="dataForm.sortNum" placeholder="序号"></el-input-number>
      </el-form-item>
      <el-form-item label="代码" prop="code">
      <el-input v-model="dataForm.code" placeholder="代码"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="字段类型" prop="fieldType">
      <el-input v-model="dataForm.fieldType" placeholder="字段类型"></el-input>
    </el-form-item>
    <el-form-item label="属性名称" prop="fieldProp">
      <el-input v-model="dataForm.fieldProp" placeholder="属性名称"></el-input>
    </el-form-item>
    <el-form-item label="标签名称" prop="label">
      <el-input v-model="dataForm.label" placeholder="标签名称"></el-input>
    </el-form-item>
    <el-form-item label="显示标签" prop="showLabel">
      <el-input v-model="dataForm.showLabel" placeholder="显示标签"></el-input>
    </el-form-item>
    <el-form-item label="扩冲属性" prop="extProp">
      <el-input v-model="dataForm.extProp" placeholder="扩冲属性"></el-input>
    </el-form-item>
    <el-form-item label="默认样式" prop="defaultStyle">
      <el-input v-model="dataForm.defaultStyle" placeholder="默认样式"></el-input>
    </el-form-item>
    <el-form-item label="是否只读" prop="isRead">
      <el-input v-model="dataForm.isRead" placeholder="是否只读"></el-input>
    </el-form-item>
    <el-form-item label="是否可见" prop="isShow">
      <el-input v-model="dataForm.isShow" placeholder="是否可见"></el-input>
    </el-form-item>
    <el-form-item label="是否必填" prop="isRequired">
      <el-input v-model="dataForm.isRequired" placeholder="是否必填"></el-input>
    </el-form-item>
    <el-form-item label="tab键顺序" prop="tabIndex">
      <el-input v-model="dataForm.tabIndex" placeholder="tab键顺序"></el-input>
    </el-form-item>
    <el-form-item label="默认值" prop="defaultValue">
      <el-input v-model="dataForm.defaultValue" placeholder="默认值"></el-input>
    </el-form-item>
    <el-form-item label="控制点ID" prop="groupId">
      <el-input v-model="dataForm.groupId" placeholder="控制点ID"></el-input>
    </el-form-item>
    <el-form-item label="最小长度" prop="minLength">
      <el-input v-model="dataForm.minLength" placeholder="最小长度"></el-input>
    </el-form-item>
    <el-form-item label="最大长度" prop="maxLength">
      <el-input v-model="dataForm.maxLength" placeholder="最大长度"></el-input>
    </el-form-item>
    <el-form-item label="表字段名" prop="tblFiled">
      <el-input v-model="dataForm.tblFiled" placeholder="表字段名"></el-input>
    </el-form-item>
    <el-form-item label="数据源" prop="dataSource">
      <el-input v-model="dataForm.dataSource" placeholder="数据源"></el-input>
    </el-form-item>
    <el-form-item label="数据字典" prop="dictCode">
      <el-input v-model="dataForm.dictCode" placeholder="数据字典"></el-input>
    </el-form-item>
    <el-form-item label="数据类型" prop="dataType">
      <el-input v-model="dataForm.dataType" placeholder="数据类型"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="description">
      <el-input type="textarea"  v-model="dataForm.description" placeholder="描述"></el-input>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/def/checkField'

    export default {
    data () {
      return {
        visible: false,
        dataForm: {

          code: '',
          name: '',
          fieldType: '',
          fieldProp: '',
          label: '',
          showLabel: '',
          extProp: '',
          defaultStyle: '',
          isRead: '',
          isShow: '',
          isRequired: '',
          tabIndex: '',
          defaultValue: '',
          groupId: '',
          minLength: '',
          maxLength: '',
          tblFiled: '',
          dataSource: '',
          dictCode: '',
          sortNum: '',
          dataType: '',
          description: ''
        },
        dataRule: {
          code: [
            { required: true, message: '代码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          fieldType: [
            { required: true, message: '字段类型不能为空', trigger: 'blur' }
          ],
          fieldProp: [
            { required: true, message: '属性名称不能为空', trigger: 'blur' }
          ],
          label: [
            { required: true, message: '标签名称不能为空', trigger: 'blur' }
          ],
          showLabel: [
            { required: true, message: '显示标签不能为空', trigger: 'blur' }
          ],
          extProp: [
            { required: true, message: '扩冲属性不能为空', trigger: 'blur' }
          ],
          defaultStyle: [
            { required: true, message: '默认样式不能为空', trigger: 'blur' }
          ],
          isRead: [
            { required: true, message: '是否只读不能为空', trigger: 'blur' }
          ],
          isShow: [
            { required: true, message: '是否可见不能为空', trigger: 'blur' }
          ],
          isRequired: [
            { required: true, message: '是否必填不能为空', trigger: 'blur' }
          ],
          tabIndex: [
            { required: true, message: 'tab键顺序不能为空', trigger: 'blur' }
          ],
          defaultValue: [
            { required: true, message: '默认值不能为空', trigger: 'blur' }
          ],
          groupId: [
            { required: true, message: '控制点ID不能为空', trigger: 'blur' }
          ],
          minLength: [
            { required: true, message: '最小长度不能为空', trigger: 'blur' }
          ],
          maxLength: [
            { required: true, message: '最大长度不能为空', trigger: 'blur' }
          ],
          tblFiled: [
            { required: true, message: '表字段名不能为空', trigger: 'blur' }
          ],
          dataSource: [
            { required: true, message: '数据源不能为空', trigger: 'blur' }
          ],
          dataType: [
            { required: true, message: '数据类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      }
    }
  }
</script>
